package keshe.view;


import keshe.ACCOUNT;
import keshe.MyObjectInputStream;
import keshe.MyObjectOutputStream;

import javax.swing.*;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class ChangePasswordJFrame extends JFrame{
    private ACCOUNT LoginAccount;
    private ArrayList<ACCOUNT> accounts=new ArrayList<>();
    private JTextField text1=new JTextField();
    private JTextField text2=new JTextField();
    public ChangePasswordJFrame(ACCOUNT account) {
        LoginAccount=account;
        ReadAccounts();
        this.setLocation(700, 300);
        this.setSize( 300, 200);
        this.setLayout(null);
        JLabel label1=new JLabel("原密码");
        label1.setBounds(50, 20, 100, 30);
        text1.setBounds(100, 20, 100, 30);
        JLabel label2=new JLabel("新密码");
        label2.setBounds(50, 70, 100, 30);
        text2.setBounds(100, 70, 100, 30);
        JButton button1=new JButton("修改");
        button1.setBounds(100, 120, 100, 30);
        button1.addActionListener(e -> {
            int i = JOptionPane.showConfirmDialog(null, "确认修改？");
            if(i==0)
            ChangePassword(LoginAccount);
        });
        this.add(label1);
        this.add(label2);
        this.add(text1);
        this.add(text2);
        this.add(button1);

        this.setVisible(true);

    }

    private void ChangePassword(ACCOUNT loginAccount) {
        String oldPassword=text1.getText();
        String newPassword=text2.getText();
        boolean flash=false;
        if(!(oldPassword.equals("")||newPassword.equals(""))) {
            for (ACCOUNT account : accounts) {
                if (account.getBankCardId().equals(loginAccount.getBankCardId())||account.getPassWord().equals(oldPassword)) {
                    account.setPassWord(newPassword);
                    flash= true;
                    break;
                }
            }
            if(!flash){
                JOptionPane.showMessageDialog(null, "原密码错误");
                return;
            }

            MyObjectOutputStream oos = null;
            try {
                oos = new MyObjectOutputStream(new FileOutputStream("AccountData/Accounts"));
                for (ACCOUNT account : accounts) {
                    oos.writeObject(account);
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        else {
            JOptionPane.showMessageDialog(null, "输入不能为空");
            return;
        }

        JOptionPane.showMessageDialog(null, "修改成功,下次登录请输入新的密码，您的新密码是"+newPassword);

    }

    //读取对象
    private void ReadAccounts(){
        try {
            MyObjectInputStream ois=new MyObjectInputStream(new FileInputStream("AccountData/Accounts"));
            //反序列化流读取文件中所有对象
            while (true) {
                try {
                    ACCOUNT account=new ACCOUNT();
                    account=(ACCOUNT)(ois.readObject());
                    accounts.add(account);
                }catch (EOFException e){
                    System.out.println("读取完成");
                    break;
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for(ACCOUNT account:accounts){
            System.out.println(account);
        }
    }
}
